package com.liyuan.yiqixing.controller;

import com.liyuan.yiqixing.Utils;
import com.liyuan.yiqixing.dto.SimpleReply;
import com.liyuan.yiqixing.model.Comment;
import com.liyuan.yiqixing.model.ResultJson;
import com.liyuan.yiqixing.model.User;
import com.liyuan.yiqixing.service.AuthenticationService;
import com.liyuan.yiqixing.service.CommentService;
import com.liyuan.yiqixing.service.DiscussionService;
import com.mysql.cj.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class PersonalController {
    @Autowired
    AuthenticationService authenticationService;
    @Autowired
    CommentService commentService;

    @Autowired
    DiscussionService discussionService;

    @GetMapping("/personal")
    public void personal(HttpServletResponse response,
                         HttpServletRequest request) throws ServletException, IOException {
        request.getRequestDispatcher("/personal.html").forward(request, response);
    }

    @PostMapping("/retrieveUserInfo")
    public ResultJson<Object> retrieveUserInfo() {
        User principal = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        principal.setPassword(null);
        return new ResultJson<>(200, "retrieve userInfo success!", principal);
    }

    @PostMapping("/retrieveReplies")
    public ResultJson<Object> retrieveReplies() {
        User principle = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Comment[] comments = commentService.retrieveCommentsByUserCommentedId(principle.getId());
        SimpleReply[] simpleReplies = transferCommentsToSimpleReplie(comments);
        return new ResultJson<>(200, "retrieve replies success!!", simpleReplies);
    }

    private SimpleReply[] transferCommentsToSimpleReplie(Comment[] comments) {
        SimpleReply[] simpleReplies = new SimpleReply[comments.length];
        for (int i = 0; i < comments.length; i++) {
            simpleReplies[i] = new SimpleReply();
            simpleReplies[i].setNickname(comments[i].getUserComment().getNickname());
            simpleReplies[i].setAvatarUrl(comments[i].getUserComment().getAvatarUrl());
            simpleReplies[i].setContent2(comments[i].getContent());
            simpleReplies[i].setModifiedAt(comments[i].getModifiedAt());
            String content1 = commentService.retrieveCommentContentByTargetIdAndType(comments[i].getTargetId(),comments[i].getType());//可能回复的是discussion，亦或是别人的评论
            simpleReplies[i].setContent1(content1);

            Integer discussionId = null;
            if(comments[i].getType().equals(Utils.EnumUtils.REPLY_TO_DISCUSSION.getType())){
                discussionId = comments[i].getTargetId();
            }else if(comments[i].getType().equals(Utils.EnumUtils.REPLY_TO_COMMENT.getType())){
                discussionId=commentService.retrieveCommentTargetIdById(comments[i].getTargetId());
            }
            simpleReplies[i].setDiscussionId(discussionId);//不论是一级评论还是二级评论，必定指向discussion
        }
        return simpleReplies;
    }
    @PostMapping("/savePersonalInfo")
    public void savePersonalInfo(@RequestParam("id") Integer id,
                                 @RequestParam("account") String account,
                                 @RequestParam("nickname") String nickname,
                                 @RequestParam("description") String description,
                                 HttpServletResponse response) throws IOException {
        User user = authenticationService.retrieveUser(id);
        if (!user.getAccount().equals(account)) {
//            return new ResultJson<>(199,"save personalInfo failed(造成这种问题很明显是因为你的id和account不一致！！)",null);
            response.sendRedirect("/personal.html?error");
        }
        user.setNickname(nickname);
        user.setDescription(description);
        authenticationService.modifyUser(user);
        response.sendRedirect("/personal.html");
    }
}
